using System;
using System.Text;

public class User
{
	public User()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public delegate void ValidationErrorHandler(StringBuilder errors);
    public static ValidationErrorHandler OnValidationErrors;

    public event EventHandler UserNameChanged;
    public event EventHandler PasswordChanged;

    private StringBuilder validationErrors;

    private string userName;
    public string UserName 
    {
        get 
        {
            return userName;
        }
        set
        {
            if (userName != value)
            {
                userName = value;
                OnUserNameChanged(EventArgs.Empty);
            }
        }
    }

    private string password;
    public string Password
    {
        get
        {
            return password;
        }
        set
        {
            if (password != value)
            {
                password = value;
                OnPasswordChanged(EventArgs.Empty);
            }
        }
    }

    private Boolean Validate()
    {
        validationErrors = new StringBuilder();
        if ((userName == null) || (userName == ""))
            validationErrors.AppendLine("A user name is required.");
        if ((password == null) || (password == ""))
            validationErrors.AppendLine("A password is required.");
        if (validationErrors.Length > 0) 
        {
            return (false);
        } else
        {
            return (true);
        }

    }

    public void Save()
    {
        if (!Validate())
        {
            if (OnValidationErrors != null)
            {
                OnValidationErrors(validationErrors);
            }
        } else
        {
            try
            {
                // do something that persists this data to the database
            } catch
            {

            }
        }
    }

    public void Load()
    {
        try
        {
            // do something that loads the data from the database
        } catch
        {

        }
    }

    private void OnUserNameChanged(EventArgs e)
    {
        if (UserNameChanged != null)
            UserNameChanged(this, e);
    }

    private void OnPasswordChanged(EventArgs e)
    {
        if (PasswordChanged != null)
            PasswordChanged(this, e);
    }
}
